import random
import requests
import os
import time

dir_path = os.path.join(os.path.dirname(__file__), 'captcha_imgs')

for i in range(40):
    brandid = '1902857'
    listid = '4459249'
    idnum = brandid + listid
    zheng = ''
    idlength = 20 - len(idnum)

    for _ in range(idlength):
        zheng += str(round(random.random() * 9))

    rnd = f'{brandid}{listid}{zheng}'

    captcha_url = f'https://v.tiantianvote.com/api/c2/captchas.png.php?rnd={rnd}&type=2&id={listid}'

    resp = requests.get(captcha_url)
    filename = os.path.join(dir_path, f'{int(time.time() * 10000)}.jpg')
    with open(filename,'wb') as f:
        f.write(resp.content)
